﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Model.WXshopBuiness;
using Xproject.Framework.Core.Repositories;
using Xproject.Framework.Utility.Extensions;

namespace Repository.WXshopBuiness
{
   public static class ShoppingCarRep
    {
        public static ShoppingCar GetShopCarByID(this IRepositoryAsync<ShoppingCar> repository, string shopCarId)
        {
            Expression<Func<ShoppingCar, bool>> express = i => i.ShopCarId == shopCarId;            
            return repository.Query().Where(express).Select().FirstOrDefault();
        }

        public static ShoppingCar GetUserShopCar(this IRepositoryAsync<ShoppingCar> repository, string productId,string userId,string specifationId,int state)
        {
            Expression<Func<ShoppingCar, bool>> express = i => i.ProductId == productId;
            express = express.AndAlso(i => i.UserId == userId);
            express = express.AndAlso(i => i.ItemState == state);
            if (!string.IsNullOrEmpty(specifationId))
            {
                express = express.AndAlso(i => i.ProductSpecificationId == specifationId);
            }
            return repository.Query().Where(express).Select().FirstOrDefault();
        }


        public static int GetUserShoppingCarCount(this IRepositoryAsync<ShoppingCar> repository, string userId)
        {
            Expression<Func<ShoppingCar, bool>> express = i => i.ItemState == 0; //0 才是没有被生成过订单的

            if (!string.IsNullOrEmpty(userId))
            {
                express = express.AndAlso(i => i.UserId == userId);
            }
            return repository.Query().Where(express).Select().Count();
        }

        public static List<ShoppingCar> GetUserShoppingCarList(this IRepositoryAsync<ShoppingCar> repository, string userId)
        {
            Expression<Func<ShoppingCar, bool>> express = i => i.ItemState== 0; //0 才是没有被生成过订单的

            if (!string.IsNullOrEmpty(userId))
            {
                express = express.AndAlso(i => i.UserId == userId);
            }
            else
            {
                return null;
            }
            return repository.Query().Where(express).Select().OrderByDescending(i=>i.CreateTime).ToList();
        }
       
    }
}
